package com.flag.liutao.modules.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.role.domain.Role;
import com.flag.liutao.modules.role.domain.RoleMenu;
import com.flag.liutao.modules.role.mapper.RoleMapper;
import com.flag.liutao.modules.role.param.RoleParam;
import com.flag.liutao.modules.role.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.role.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 获取角色列表
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @Override
    public Map<String, Object> getRolePage(int page, int limit, String keyword) {
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(keyword)){
            lqw.like(Role::getRoleName,keyword).or().like(Role::getRoleKey,keyword);
        }
        IPage<Role> ipage  = new Page<>(page,limit);
        IPage<Role> pageDB = roleMapper.selectPage(ipage, lqw);
        Map<String,Object> map = new HashMap<>();
        List<RoleVo> list = pageDB.getRecords().stream().map(role->{
            RoleVo vo = new RoleVo();
            BeanUtil.copyProperties(role,vo);
            return vo;
        }).collect(Collectors.toList());
        map.put("total",pageDB.getTotal());
        map.put("content",list);
        return map;
    }

    /**
     * 添加角色信息
     * @param param
     */
    @Override
    public void addRole(RoleParam param) {
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleKey,param.getRoleKey());
        Role role = roleMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(role)){
            throw new MyException("角色标识--"+param.getRoleKey()+"--已被使用");
        }
        Role roleDB = new Role();
        BeanUtil.copyProperties(param,roleDB);
        roleMapper.insert(roleDB);
    }

    /**
     * 修改角色
     * @param param
     */
    @Override
    public void EditRole(RoleParam param) {
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleKey,param.getRoleKey());
        Role role = roleMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(role) && param.getId()!=role.getId()){
            throw new MyException("角色标识--"+param.getRoleKey()+"--已被使用");
        }
        Role roleDB = new Role();
        BeanUtil.copyProperties(param,roleDB);
        roleMapper.updateById(roleDB);
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public void delRole(Long id) {
        roleMapper.deleteById(id);
    }

    @Override
    public List<RoleVo> getRoleList() {
        List<Role> roleList = roleMapper.selectList(null);
        List<RoleVo> convert = Convert.convert(new TypeReference<List<RoleVo>>() {
        }, roleList);
        return convert;
    }

}
